﻿@using A2.ERP.Models
@{
    //库房
    List<t_StoreHouse> houselist = ViewBag.HouseList;
    //库区
    List<t_StoreHouseDetail> detail = ViewBag.HouseDetail;
    //订单信息
    t_BuyOrder order = ViewBag.order;
    //供应商
    List<t_Supplier> supplier = ViewBag.supplier;
    //商品信息
    List<t_Products> product = ViewBag.product;
}
<section id="ModuleBuyOrder">
    <style type="text/css">
        #BuyOrder input[type='text'], #BuyOrder input[type='date'], #BuyOrder input[type='number'], select {
            margin-right: 100px;
        }

        #BuyOrder input.long {
            width: 500px;
            margin-right: 30px;
        }

        #AllOver {
            width: 100px;
            display: none;
        }

        .select {
            margin: 0px;
        }
    </style>

    <div id="BuyOrder">
        <div class="card-header">添 加 采 购 订 单</div>
        <div class="card-body">
            @using (Html.BeginForm("ModuleBuyOrder", "Purchase", FormMethod.Post, new
            {
                @class = "form addBuyOrder"
            }))
            {
                <div class="form-group">
                    <div class="input-group mb-3">
                        <div class="input-group-prepend">
                            <span class="input-group-text">订单编号</span>
                        </div>
                        <input type="text" name="BuyOrderID" value="@order.BuyOrderID">
                        <div class="input-group-prepend">
                            <span class="input-group-text">制单日期</span>
                        </div>
                        <input type="date" name="BuyOrderDate">
                        <div class="input-group-prepend">
                            <span class="input-group-text">库房</span>
                        </div>

                        <select class="select nomargin" name="StoreHouseID">
                            @foreach (var item in houselist)
                            {
                                <option value="@item.HouseID" onclick="ChangeHouse(@item.HouseID)">@item.HouseName</option>
                            }
                        </select>

                        <div class="input-group-prepend">
                            <span class="input-group-text">库区</span>
                        </div>
                        <select class="select" name="HouseDetailID">
                            @foreach (var item in detail)
                            {
                                <option value="@item.ID">@item.SubareaName</option>
                            }
                        </select>

                    </div>

                </div>
                <div class="form-group">
                    <div class="input-group mb-3">
                        <div class="input-group-prepend">
                            <span class="input-group-text">业务代表</span>
                        </div>
                        <input type="text" name="UserName">
                        <div class="input-group-prepend">
                            <span class="input-group-text">签订日期</span>
                        </div>
                        <input type="date" name="SignDate" />

                        <div class="input-group-prepend">
                            <span class="input-group-text">交货日期</span>
                        </div>
                        <input type="date" name="TradeDate">


                    </div>

                </div>
                <div class="form-group">
                    <div class="input-group mb-3">

                        <div class="input-group-prepend">
                            <span class="input-group-text">合计金额</span>
                        </div>
                        <input type="number" name="TotalPrice" class="number">
                        <div class="input-group-prepend">
                            <span class="input-group-text">交货地址</span>
                        </div>
                        <input type="text" name="TradeAddress">

                    </div>

                </div>
                <div class="form-group">
                    <div class="input-group mb-3">
                        <div class="input-group-prepend">
                            <span class="input-group-text">备注</span>
                        </div>
                        <input type="text" name="Description" class="long">
                        <input type="submit" class="btn btn-primary" value="保存" id="Add" />
                        <input type="button" class="btn btn-primary" value="完成所有" id="AllOver" />
                    </div>
                </div>
            }
            @* 商品 *@
            <div class="card-header">商 品 信 息</div>
            <div class="card-body">
                <div class="form-group">
                    <div class="input-group mb-3">
                        <div class="input-group-prepend">
                            <span class="input-group-text">商品</span>
                        </div>
                        <select name="ProductsId">
                            @foreach (var item in product)
                            {
                                <option value="@item.ProductsID">@item.ProductsName</option>
                            }
                        </select>
                        <div class="input-group-prepend">
                            <span class="input-group-text">价钱</span>
                        </div>
                        <input type="number" name="Price" class="number">
                        <div class="input-group-prepend">
                            <span class="input-group-text">数量</span>
                        </div>
                        <input type="number" name="Quantity" class="number">
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group mb-3">
                        <div class="input-group-prepend">
                            <span class="input-group-text">税率%</span>
                        </div>
                        <input type="number" name="TaxRate">
                        <div class="input-group-prepend">
                            <span class="input-group-text">折扣率</span>
                        </div>
                        <input type="number" class="number" name="DiscountRate">
                        <div class="input-group-prepend">
                            <span class="input-group-text">供应商</span>
                        </div>
                        <select class="select" name="SupplierId">
                            @foreach (var item in supplier)
                            {
                                <option value="@item.SupplierID">@item.SupplierName</option>
                            }
                        </select>
                        <div>
                        </div>
                    </div>
                </div>

                <div class="input-group mb-3">
                    <div class="input-group-prepend">
                        <span class="input-group-text">描述</span>
                    </div>
                    <input type="text" name="Description" class="long">
                    <input type="button" class="btn btn-primary btn-save" value="保存" />
                    <div>
                    </div>
                </div>

            </div>

            <div id="Data">
                <table class="table table-bordered products-detail">
                    <thead>
                        <tr>
                            <th>商品编号</th>
                            <th>商品名称</th>
                            <th>供应商</th>
                            <th>数量</th>
                            <th>采购额</th>
                            <th>折扣额</th>
                            <th>税额</th>
                            <th>金额</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>

        </div>


</section>
<script type="text/javascript">
    ;
    !function () {
        var $addBuyOrderFrm = $('#ModuleBuyOrder .addBuyOrder');
        $addBuyOrderFrm.submit(function (e) {
            e.preventDefault();
            $addBuyOrderFrm.ajaxSubmit({
                type: 'POST',
                dataType: 'json',
                success: function (e) {
                    $("#Add").css('display', 'none');
                    $("#AllOver").css('display', 'block');
                    //$tab.reloadTab('#ModuleBuyOrder');
                }
            });
        });
        var $productsDetail = $('.products-detail');

        var tableTemp = '<tr><td>{ProductsId}</td><td>{ProductsName}</td><td>{SupplierId}</td><td>{Quantity}</td><td>0</td><td>{Price}</td><td>{TaxRate}</td><td>{DiscountRate}</td><td><button class="btn btn-success">删除</button></td></tr>';

        var $btnSave = $('.btn-save');

        $btnSave.on('click', function () {
            var html = tableTemp.replace(/{(.+?)}/g, function (e, $1) {
                if ($1 === 'ProductsId') {
                    return $('select[name=ProductsId').val();
                } else if ($1 === 'ProductsName') {
                    return $("select[name=ProductsId] option:selected").text()                    
                } else if ($1 === 'SupplierId') {
                    return $('select[Name=SupplierId').val();
                }
                return $('input[name=' + $1).val();
            });
            $productsDetail.append(html);
        });

        $('#AllOver').on('click', function () {
            alert('添加成功');

            $tab.reloadTab('#ModuleBuyOrder');
        })
    }();
</script>
